package com.itheima.health.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.CheckGroupDao;
import com.itheima.health.dao.CheckGroupItemDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckGroupItem;
import com.itheima.health.service.CheckGroupService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: liangWeiQUQ
 * @Description: TODO
 * @DateTime: 2021/2/26 23:01
 **/
@DubboService
@Slf4j
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Autowired
    private CheckGroupItemDao checkGroupItemDao;

    /**
     * @Author: liangWeiQUQ
     * @Date: 23:02 2021/2/26
     * @Parms
     * @ReturnType:
     * @Description: 检查组-分页查询
     */

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.调用分页的插件
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.封装查询的条件
        String queryString = queryPageBean.getQueryString();
        Example example = new Example(CheckGroup.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(queryString)) {
            criteria.orLike("code", "%" + queryString + "%");
            criteria.orLike("name", "%" + queryString + "%");
            criteria.orLike("helpCode", "%" + queryString + "%");
        }
        //查询
        Page<CheckGroup> page = (Page<CheckGroup>) checkGroupDao.selectByExample(example);

        return new PageResult(page.getTotal(), page.getResult());
    }
    /**
     * @Author: liangWeiQUQ
     * @Date: 15:00 2021/2/27
     * @Parms
     * @ReturnType:
     * @Description: 检查组--新增检查组
    */
    @Override
    public void add(Integer[] checkitemIds, CheckGroup checkGroup) {
        //1.新增检查组
        checkGroupDao.insertSelective(checkGroup);
        //2.维护中间关联关系表
        for (Integer checkitemId : checkitemIds) {
            CheckGroupItem checkGroupItem = new CheckGroupItem();
            checkGroupItem.setCheckitemId(checkitemId);
            checkGroupItem.setCheckgroupId(checkGroup.getId());
            checkGroupItemDao.insertSelective(checkGroupItem);
        }
    }
    /**
     * @Author: liangWeiQUQ
     * @Date: 15:10 2021/2/27
     * @Parms
     * @ReturnType:
     * @Description: [检查组-根据id查询检查组]
    */
    @Override
    public CheckGroup findById(Integer id) {
        log.info("[检查组-根据id查询检查组] id: {}", id);
        return checkGroupDao.selectByPrimaryKey(id);
    }
    /**
     * @Author: liangWeiQUQ 
     * @Date: 15:10 2021/2/27
     * @Parms
     * @ReturnType: 
     * @Description: 根据检查组id查询关联检查项信息
    */
    @Override
    public List<Integer> findCheckItemIdsById(Integer id) {
        log.info("[检查组-根据检查组id查询关联检查项信息] id: {}", id);
        CheckGroupItem checkGroupItem = new CheckGroupItem();
        checkGroupItem.setCheckgroupId(id);
        List<CheckGroupItem> checkGroupItems = checkGroupItemDao.select(checkGroupItem);
        List<Integer> checkItemIds = new ArrayList<>();
        for (CheckGroupItem groupItem : checkGroupItems) {
            checkItemIds.add(groupItem.getCheckitemId());
        }
        return checkItemIds;
    }
    /**
     * @Author: liangWeiQUQ
     * @Date: 15:10 2021/2/27
     * @Parms [checkitemIds, checkGroup]
     * @ReturnType: com.itheima.health.entity.Result
     * @Description: 检查组-修改数据
     */
    @Override
    @Transactional
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        log.info("[检查组-修改数据]checkitemids: {},  checkGroup: {}", checkitemIds, checkGroup);

        //1.修改检查组
        checkGroupDao.updateByPrimaryKeySelective(checkGroup);
        //2.维护中间关联关系表
        //2.1 根据groupId将原先的全部删除
        //checkGroupItemMapper.deleteByPrimaryKey(checkGroup.getId());
        CheckGroupItem checkGroupItem = new CheckGroupItem();
        checkGroupItem.setCheckgroupId(checkGroup.getId());
        checkGroupItemDao.delete(checkGroupItem);
        //2.2 新增
        for (Integer checkitemId : checkitemIds) {
            CheckGroupItem groupItem = new CheckGroupItem();
            groupItem.setCheckitemId(checkitemId);
            groupItem.setCheckgroupId(checkGroup.getId());
            checkGroupItemDao.insertSelective(groupItem);
        }
    }
    /**
     * @Author: liangWeiQUQ
     * @Date: 17:27 2021/2/27
     * @Parms
     * @ReturnType:
     * @Description: 删除检查组的数据
    */
    @Override
    public void deleteById(Integer id) {
        log.info("[检查组-删除数据]data: {}", id);
        checkGroupDao.deleteByPrimaryKey(id);
    }
    /**
     * @Author: liangWeiQUQ
     * @Date: 17:27 2021/2/27
     * @Parms
     * @ReturnType:
     * @Description: 查询所有检查组
    */
    @Override
    public List<CheckGroup> findAll() {

        log.info("[检查组-查询所有检查组");
        return checkGroupDao.selectAll();
    }
}
